package com.mdd.maduoduo.utils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class CommonAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_EMPTY = 0;   // 空布局类型
    private static final int TYPE_NORMAL = 1;  // 正常布局类型

    protected List<T> mData = new ArrayList<>();
    private View mEmptyView;
    private int mEmptyLayoutResId = -1;

    // 点击事件监听器
    private OnItemClickListener<T> mOnItemClickListener;
    private OnItemLongClickListener<T> mOnItemLongClickListener;
    private OnItemChildClickListener<T> mOnItemChildClickListener;

    @Override
    public int getItemViewType(int position) {
        if (isEmpty()) {
            return TYPE_EMPTY;
        }
        return getCustomItemViewType(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_EMPTY) {
            // 空布局
            View view = mEmptyView != null ? mEmptyView :
                    LayoutInflater.from(parent.getContext())
                            .inflate(mEmptyLayoutResId, parent, false);
            return new EmptyViewHolder(view);
        }
        // 正常布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(getLayoutResId(viewType), parent, false);
        return new CommonViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof CommonViewHolder && !isEmpty()) {
            CommonViewHolder commonHolder = (CommonViewHolder) holder;
            T item = mData.get(position);
            bindData(commonHolder, item, position);

            // 设置点击事件
            if (mOnItemClickListener != null) {
                holder.itemView.setOnClickListener(v -> {
                    mOnItemClickListener.onItemClick(this, v, position, item);
                });
            }

            // 设置长按事件
            if (mOnItemLongClickListener != null) {
                holder.itemView.setOnLongClickListener(v -> {
                    return mOnItemLongClickListener.onItemLongClick(this, v, position, item);
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return isEmpty() ? 1 : mData.size();
    }

    // ================== 抽象方法 ==================

    /**
     * 获取布局资源ID
     */
    @LayoutRes
    protected abstract int getLayoutResId(int viewType);

    /**
     * 绑定数据
     */
    protected abstract void bindData(@NonNull CommonViewHolder holder, T item, int position);

    // ================== 数据操作 ==================

    public void setData(@Nullable List<T> data) {
        mData = data != null ? data : new ArrayList<>();
        notifyDataSetChanged();
    }

    public void addData(@NonNull List<T> data) {
        int start = mData.size();
        mData.addAll(data);
        notifyItemRangeInserted(start, data.size());
    }

    public void addData(T item) {
        mData.add(item);
        notifyItemInserted(mData.size() - 1);
    }

    public void remove(int position) {
        if (position >= 0 && position < mData.size()) {
            mData.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public List<T> getData() {
        return mData;
    }

    public T getItem(int position) {
        return position >= 0 && position < mData.size() ? mData.get(position) : null;
    }

    // ================== 空布局相关 ==================

    public void setEmptyView(View emptyView) {
        this.mEmptyView = emptyView;
        this.mEmptyLayoutResId = -1;
        notifyDataSetChanged();
    }

    public void setEmptyView(@LayoutRes int emptyLayoutResId) {
        this.mEmptyLayoutResId = emptyLayoutResId;
        this.mEmptyView = null;
        notifyDataSetChanged();
    }

    private boolean isEmpty() {
        return (mEmptyView != null || mEmptyLayoutResId != -1) && mData.isEmpty();
    }

    // ================== 点击事件 ==================

    public interface OnItemClickListener<T> {
        void onItemClick(CommonAdapter<T> adapter, View view, int position, T item);
    }

    public interface OnItemLongClickListener<T> {
        boolean onItemLongClick(CommonAdapter<T> adapter, View view, int position, T item);
    }

    public interface OnItemChildClickListener<T> {
        void onItemChildClick(CommonAdapter<T> adapter, View view, int position, T item);
    }

    public void setOnItemClickListener(OnItemClickListener<T> listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener<T> listener) {
        this.mOnItemLongClickListener = listener;
    }

    public void setOnItemChildClickListener(OnItemChildClickListener<T> listener, int... viewIds) {
        this.mOnItemChildClickListener = listener;
    }

    // ================== 其他功能 ==================

    protected int getCustomItemViewType(int position) {
        return TYPE_NORMAL;
    }

    // 空布局 ViewHolder
    private static class EmptyViewHolder extends RecyclerView.ViewHolder {
        EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }
}
